//
//  SelectProductListController.m
//  LianShangBao
//
//  Created by 主事丫环HU on 2020/9/4.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "SelectProductListController.h"
#import "SelectProductListCell.h"

@interface SelectProductListController ()
@property (weak, nonatomic) IBOutlet UIView *bottomBgView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataArray;
@end

@implementation SelectProductListController

- (instancetype)init {
    if (self = [super initWithNibName:nil bundle:nil]) {
        self.modalPresentationStyle = UIModalPresentationCustom;
        self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    self.dataArray = [NSMutableArray arrayWithCapacity:0];
    [self requestProductData];
}
- (void)setupUI{
     self.view.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    // 切上边两个圆角
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bottomBgView.bounds
                                                   byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bottomBgView.bounds;
    maskLayer.path = maskPath.CGPath;
    self.bottomBgView.layer.mask = maskLayer;
    CGFloat radius = 10;
    self.bottomBgView.layer.cornerRadius = radius;
    // 上面的方法只能将左上角切圆角，下面的方法是切右上角圆角
    if (@available(iOS 11.0, *)) {
        self.bottomBgView.layer.maskedCorners = kCALayerMaxXMinYCorner;
    } else {
        // Fallback on earlier versions
    }
    self.tableView.rowHeight = 50;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [[UIView alloc] init];
}
- (void)requestProductData{
    [NetWorkTool POST:getGoodsAll param:nil success:^(id dic) {
        self.dataArray = [SelectProductListModel mj_objectArrayWithKeyValuesArray:dic[@"data"]];
        [self.tableView reloadData];
    } other:^(id dic) {
        
    } fail:^(NSError *error) {
        
    } needUser:YES];
}
#pragma mark - tableview datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SelectProductListModel *model = self.dataArray[indexPath.row];
    SelectProductListCell *cell = [SelectProductListCell cellWithTableView:tableView];
    cell.model = model;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SelectProductListModel *model = self.dataArray[indexPath.row];
    if (self.selectedProductClick) {
        self.selectedProductClick(model);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
